package com.bosssoft.trainee.nontax.utils;/*
部门树工具类
@author shencanting
@date 2022/06/201555
**/

import com.bosssoft.trainee.nontax.entity.po.DepartmentPO;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author shencanting
 */
public class DepartmentTree {

    private DepartmentTree(){}
    /**
     * 生成部门树列表
     * @param deptList 部门列表
     * @param pid 父级部门id
     * @return
     */
    public static List<DepartmentPO> makeDepartmentTree(List<DepartmentPO> deptList, Long pid){
        List<DepartmentPO> list = new ArrayList<>();
        Optional.ofNullable(deptList).orElse(new ArrayList<>())
                .stream().filter(item -> item != null && Objects.equals(item.getPid(),pid))
                .forEach(item ->{
                    DepartmentPO dept = new DepartmentPO();
                    BeanUtils.copyProperties(item, dept);
                    //获取每一个item的下级部门,递归生成部门树
                    List<DepartmentPO> children = makeDepartmentTree(deptList,item.getId());
                    dept.setChildren(children);
                    list.add(dept);
                });
        return list;
    }
}
